package com.wz.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class Client {

	public static void main(String[] args) {
        EventLoopGroup workerGroup = new NioEventLoopGroup();  
  
        try {  
            Bootstrap b = new Bootstrap(); // (1)  
            b.group(workerGroup); // (2)  
            b.channel(NioSocketChannel.class); // (3)  
            b.option(ChannelOption.SO_KEEPALIVE, true); // (4)  
            b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 20000);
            b.handler(new ChannelInitializer<SocketChannel>() {
            	
                @Override
				public void channelActive(ChannelHandlerContext ctx) throws Exception {
                	System.out.println("cli-init-channelActive");
                	super.channelActive(ctx);
				}
	
				public void channelInactive(ChannelHandlerContext ctx) throws Exception {
					System.out.println("cli-init-channelInactive");
					super.channelInactive(ctx);
				}

				@Override
				public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
					System.out.println("cli-init-channelRead");
					super.channelRead(ctx, msg);
				}

				@Override
				public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
					System.out.println("cli-init-channelReadComplete");
					super.channelReadComplete(ctx);
				}

				@Override
				public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
					System.out.println("cli-init-channelUnregistered");
					super.channelUnregistered(ctx);
				}

				@Override
				public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
					System.out.println("cli-init-channelWritabilityChanged");
					super.channelWritabilityChanged(ctx);
				}

				@Override
				public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
					System.out.println("cli-init-exceptionCaught");
					super.exceptionCaught(ctx, cause);
				}

				@Override
				public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
					System.out.println("cli-init-userEventTriggered");
					super.userEventTriggered(ctx, evt);
				}

				@Override  
                public void initChannel(SocketChannel ch) throws Exception {
					System.out.println("cli-init-initChannel");
                    ch.pipeline().addLast(new ClientHandler());
                }
            });
            	// Start the client.  
            	ChannelFuture f = null;
				try {
					f = b.connect("127.0.0.1", 8001).sync();
	            	f.channel().closeFuture().sync();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} // (5) 
        } finally {
            workerGroup.shutdownGracefully();  
        }
	}
}